import java.util.Random;
import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        //给定两个数，求这两个数的最大公约数
        Scanner s = new Scanner(System.in);
        System.out.println("请输入两个整数：");
        int num1 = s.nextInt();
        int num2 = s.nextInt();
        int r = 0;

        while((r = num1 % num2) != 0){
            num1 = num2;
            num2 = r;
        }
        System.out.println(num2);
    }
    public static void main15(String[] args) {
        //获取一个数二进制序列中所有的偶数位和奇数位， 分别输出二进制序列
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个数字：");
        int num = s.nextInt();
        //先输出偶数位
        int i = 0;
        System.out.println("偶数位：");
        for (i = 0; i < 32; i++) {
            if(i % 2 == 0){
                System.out.print(((num >> i) & 1) + " ");
            }
        }
        //再输出奇数位
        System.out.println("奇数位：");
        for (i = 0; i < 32; i++) {
            if(i % 2 != 0){
                System.out.print(((num >> i) & 1) + " ");
            }
        }
    }
    public static void main14(String[] args) {
//        编写代码模拟三次密码输入的场景。
//        最多能输入三次密码，密码正确，提示“登录成功”,密码错误， 可以重新输 入，最多输入三次。三次均错，则提示退出程序
        Scanner s = new Scanner(System.in);
        String password = "abc123";
        int count = 0;
        while(count < 3){
            System.out.println("请输入密码：");
            String input = s.nextLine();
            if(input.compareTo(password) == 0){
                break;
            }
            count++;
        }
        if(count >= 3){
            System.out.println("退出程序");
        }else {
            System.out.println("登录成功");
        }
    }
    public static void main13(String[] args) {
        //输出一个整数的每一位，如：123的每一位是3，2，1
        Scanner s = new Scanner(System.in);
        int num = s.nextInt();
        while(num > 0){
            System.out.print(num % 10 + " ");
            num /= 10;
        }
    }
    public static void main12(String[] args) {
        //猜数字
        int num = 0;
        int guess = 0;
        Random r = new Random();
        num = r.nextInt(101);
        //System.out.println(num);
        Scanner s = new Scanner(System.in);

        while(true){
            System.out.print("请输入要猜的数字：");
            guess = s.nextInt();
            if(guess > num) {
                System.out.println("猜大了");
            }else if(guess < num) {
                System.out.println("猜小了");
            }else {
                System.out.println("猜对了");
                break;
            }
        }
    }
    public static void main11(String[] args) {
        //计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
        float sum = 0.0f;
        for (int i = 1; i <= 100; i++) {
            sum += (1.0f / i) * Math.pow(-1, i - 1);
        }
        System.out.println(sum);
    }
    public static void main10(String[] args) {
        //编写程序数一下 1到 100 的所有整数中出现多少个数字9
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            //各位出现数字9
            if(i % 10 == 9){
                count++;
//                System.out.print(i + " ");
            }
            //十位出现数字9
            if(i / 10 == 9){
                count++;
//                System.out.print(i + " ");
            }
        }
        System.out.println(count);
    }
    public static void main9(String[] args) {
        //求一个数中的二进制个数
        int num = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个数字：");
        num = s.nextInt();
        int count = 0;

        while(num > 0){
            num &= (num - 1);
            count++;
        }
        System.out.println(count);
    }
    public static void main8(String[] args) {
        //获取一个数二进制序列的所有奇数位和偶数位，分别输出二进制序列
        int num = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个数字：");
        num = s.nextInt();

        for (int i = 0; i < 32; i++) {
            if(i % 2 != 0){
                System.out.print(((num >> i) & 1) + " ");
            }
        }
        System.out.println("");
        for (int j = 0; j < 32; j++) {
            if(j % 2 != 0){
                continue;
            }else {
                System.out.print(((num >> j) & 1) + " ");
            }
        }
    }
    public static void main7(String[] args) {
        //求二进制中1的个数
        int num = 0;
        int count = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个整数：");
        num = s.nextInt();

        for (int i = 0; i < 32; i++) {
            if((num & 1) == 1){
                count++;
            }
            num >>= 1;
        }

        System.out.println(count);
    }
    public static void main6(String[] args) {
        //求0-999999之间的水仙花数
        int num = 0;
        for (int i = 0; i < 1000000; i++) {
            //首先需要确定i是几位数
            int n = 0;
            num = i;
            while(num > 0){
                num /= 10;
                n++;
            }
            int sum = 0;
            num = i;
            while(num > 0){
                sum += Math.pow(num % 10, n);//取最后一位
                num /= 10;//去掉1位
            }
            if(sum == i){
                System.out.println(i);
            }

        }
    }
    public static void main5(String[] args) {
        //求两个数的最大公约数
        int num1 = 0;
        int num2 = 0;
        int r = 1;

        Scanner s = new Scanner(System.in);
        System.out.print("请输入两个整数：");
        num1 = s.nextInt();
        num2 = s.nextInt();
        while((r = num1 % num2) != 0){
            //r = num1 % num2;
            num1 = num2;
            num2 = r;
        }
        System.out.println("最大公约数为" + num2);
    }
    public static void main4(String[] args) {
        //输出乘法口诀表
        int i = 1;
        int j = 1;
        for (i = 1; i < 10; i++) {
            for (j = 1; j <= i; j++) {
                System.out.print(j + "x" + i + "=" + j*i + "  ");
            }
            System.out.println("");
        }
    }
    public static void main3(String[] args) {
        //求1000~2000年之间的闰年
        for (int i = 1000; i < 2000; i++) {
            if((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0)){
                System.out.println(i + "是润年");
            }
        }
    }
    public static void main2(String[] args) {
        //找出1~100之间的所有素数
        int num = 0;
        int i = 0;
        for (i = 2; i < 100; i++) {
            int j = 0;
            for (j = 2; j <= Math.sqrt(i); j++) {
                if(i % j == 0){
                    break;
                }
            }
            if(j > Math.sqrt(i)){
                System.out.println(i + "是素数");
            }
        }
    }
    public static void main1(String[] args) {
        //写一个猜数字游戏
        int guess = 0;
        int num = 0;
        //生成随机数
        Random r = new Random();
        num = r.nextInt(101);
        System.out.println(num);
        Scanner s = new Scanner(System.in);
        while(true){
            System.out.println("请输入你要猜的数字：");
            guess = s.nextInt();
            if(guess == num){
                System.out.println("猜对了");
                break;
            } else if(guess > num){
                System.out.println("猜大了");
            } else {
                System.out.println("猜小了");
            }
        }

    }
}
